package utils

import (
	"mvip-route/config"
	"mvip-route/service/damRed/impl"
	"mvip-route/service/model"
	"net/http"
	"time"
)

// LogAsync 异步记录日志
func LogAsync(message string, clientIP string, platform string, req *http.Request) {
	go func() {
		service := impl.LogService{}
		region, err := config.Searcher.SearchByStr(clientIP)
		if err != nil {
			region = "未知"
		}
		log := model.Log{
			UserAgent: req.Header.Get("User-Agent"),
			Url:       req.RequestURI,
			Ip:        clientIP,
			Region:    region,
			Platform:  platform,
			Context:   message,
			Referer:   req.Referer(),
			Time:      time.Now(),
		}
		service.SaveLog(&log)
	}()
}
